package com.itheima.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.pojo.Category;
import com.itheima.pojo.Employee;
import com.itheima.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 分类管理
 */
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService service;
    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R pageSelect(@RequestParam("page") Integer page,@RequestParam("pageSize") Integer pageSize){
        Page<Category> categoryPage = new Page<>(page,pageSize);
        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<>();
        lqw.orderByAsc(Category::getSort);
        service.page(categoryPage,lqw);
        return R.success(categoryPage);
    }

    /**
     * 新增分类
     * @param category
     * @return
     */
    @PostMapping
    public R addCategory(@RequestBody Category category){
        boolean save = service.save(category);
        return R.success(save);
    }

    /**
     * 删除分类
     * @param id
     * @return
     */
    @DeleteMapping
    public R deleteCategory(@RequestParam("id") Long id){
        return service.checkDelete(id);
    }

    /**
     * 修改分类
     * @param category
     * @return
     */
    @PutMapping
    public R updateCategory(@RequestBody Category category){
        boolean updateById = service.updateById(category);
        return R.success(updateById);
    }

    /**
     * 获取菜品分类
     * @param type
     * @return
     */
    @GetMapping("/list")
    public R selectCategoryType(Integer type){
        List<Category> list = service.getCategoryType(type);
        return R.success(list);
    }
}
